package com.xxx.modules.controller;;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxx.modules.entity.Comment;
import com.xxx.modules.service.CommentService;
import com.xxx.modules.service.NoticeService;
import com.xxx.modules.entity.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;



/**
 * notice 公告
 *
 * @author 北易航
 * @since 1.0.0 2022-12-19
 */
@RestController
@RequestMapping("/common/notice")
@Api(tags="notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private CommentService commentService;

    /**
    *  查询所有notice
    * @param pageNum
    * @param pageSize
    * @param notice
    * @return
    */


    @GetMapping("/getNoticeInfo")
    @ApiOperation("获取所有公告")

    public Result getNoticeInfo(Notice notice, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Notice> data = noticeService.selectNoticeInfo(notice,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }

    /**
     * 根据id获取notice
     * @param id
     * @return
     */

    @GetMapping("/getIdNoticeInfo")
    public Result getIdNoticeInfo( Integer id){
        Notice notice =  noticeService.getById(id);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.setSql("clicknum = clicknum + 1");
        wrapper.eq("id",id);
        noticeService.update(wrapper);
        // 查询评论数量
        int commentNum = commentService.count((Wrapper<Comment>) new QueryWrapper().eq("notice_id", id));
        notice.setCommentNum(commentNum);
        noticeService.updateNoticeInfo(notice);
        System.out.println(commentNum);

        wrapper.setSql("comment");

        return ResultUtil.success(1,"成功",notice);
    }

    /**
    * 保存notice
    * @param notice
    * @return
    */
    @PostMapping("/saveNoticeInfo")
    @ApiOperation("保存公告")
    public Result saveNoticeInfo(@RequestBody Notice notice){
        noticeService.saveNoticeInfo(notice);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新notice
     * @param notice
     * @return
     */

    @PutMapping("/updateNoticeInfo")
    @ApiOperation("更新")
    public Result updateNoticeInfo(@RequestBody Notice notice){
        noticeService.updateNoticeInfo(notice);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除notice
     * @param id
     * @return
     */
    @DeleteMapping("/delNoticeInfo")
    public Result delNoticeInfo(Integer id){
        noticeService.delNoticeInfo(id);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 根据id集合删除notice
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchNoticeInfo")
    public Result delBatchNoticeInfo(String idList){
        noticeService.delBatchNoticeInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }




}